<?php

class UnitController extends RController
{
	/**
	 * Declares class-based actions.
	 */
	 
	public $defaultAction = 'admin';
	public $layout='//layouts/lay_column2';
    public $menu=array();
    public $moduleName="单位";	//此处修改本模块名称
    
    public function init()
	{
    	$leftmenu = 'system';	//此处修改模块所属菜单
		$this->menu = Yii::app()->params[$leftmenu];
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
	
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionAdmin()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('admin',array(
			'user' => User::model()->findByPk(Yii::app()->user->id),
		));
	}

	public function actionOp()
	{
		$status = true;
		$rs = array(
			'no' => '-1',
			'data' => array(),
			'error' => '参数错误',
		);
		if(isset($_POST) && isset($_POST['id']) 
						 && isset($_POST['name'])
						 && isset($_POST['type'])
						 && isset($_POST['op'])){
			$id = $_POST['id'];
			$name = $_POST['name'];
			$type = $_POST['type'];
			$op = $_POST['op'];
			
			if($op != 'delete'){
				switch($type){
					case 'PROVINCE':					
						$model = MtsysProvince::model()->findByPk($id);
						if($op == 'add'){
							$model = new MtsysProvince;
						}
						$model->PROVINCE_NAME = $name;
						break;
					case 'CITY':
						$model = MtsysCity::model()->findByPk($id);
						if($op == 'add'){
							$model = new MtsysCity;						
							$model->PROVINCE_ID = $id;
						}
						$model->CITY_NAME = $name;	
						break;
					case 'COUNTY':					
						$model = MtsysCounty::model()->findByPk($id);
						if($op == 'add'){
							$model = new MtsysCounty;
							$model->CITY_ID = $id;
						}
						$model->COUNTY_NAME = $name;
						break;
					case 'BASE':
						$model = MtsysBase::model()->findByPk($id);
						$model->BASE_NAME = $name;
						break;	
				}				
				$status = $model->save();	
			}else{
				$users = User::model()->findAll();
				switch($type){
					case 'PROVINCE':
						$citys = MtsysCity::model()->findAll("PROVINCE_ID=:PID",array(":PID"=>$id));
						if(!empty($citys)){
							foreach($citys as $city){
								foreach($users as $user){
									if($user->unit_id == $city->CITY_ID && $user->unit_level == 2){
										$user->delete();
									}
								}								
								$countys = MtsysCounty::model()->find("CITY_ID=:CID",array(":CID"=>$city->CITY_ID));
								if(!empty($countys)){
									foreach($countys as $county){
										foreach($users as $user){
											if($user->unit_id == $county->COUNTY_ID && $user->unit_level == 3){
												$user->delete();
											}
										}
									}
									$status = MtsysCounty::model()->deleteAll("CITY_ID=:CID",array(":CID"=>$city->CITY_ID));									
								}
								$status = MtsysCity::model()->findByPk($city->CITY_ID)->delete();	
							}
								
						}	
						
						if(MtsysProvince::model()->findByPk($id)){
							foreach($users as $user){
								if($user->unit_id == $id && $user->unit_level == 1){
									$user->delete();
								}
							}
							$status = MtsysProvince::model()->findByPk($id)->delete();
							//$status &= MtsysBase::model()->deleteAll("PROVINCE_ID=:PID",array(
								//":PID"=>$id));

							$bases = MtsysBase::model()->findAll("PROVINCE_ID=:PID",array(
								":PID"=>$id));
							if(!empty($bases)){
								$status &= MtsysBase::model()->deleteAll("PROVINCE_ID=:PID",array(
									":PID"=>$id));
								foreach($bases as $base){  
									foreach($users as $user){
										if($user->unit_id == $base->BASE_ID && $user->unit_level == 4){
											$user->delete();
										}
									}
									if(BtBatterys::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
										$status &= BtBatterys::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
									}
									if(BtBattery::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
										$status &= BtBattery::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
									}
									if(BtRundata::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
										$status &= BtRundata::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
									}
									if(Pickdata::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
										$status &= Pickdata::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
									}
									if(Sensors::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
										$status &= Sensors::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
									}
								}
							}
						}
						break;	
					case 'CITY':
						
			if(MtsysCity::model()->findByPk($id)){
				foreach($users as $user){
					if($user->unit_id == $id && $user->unit_level == 2){
						$user->delete();
					}
				}
				$status = MtsysCity::model()->findByPk($id)->delete();					
			}
			$countys = MtsysCounty::model()->findAll("CITY_ID=:CID",array(":CID"=>$id));
			if(MtsysCounty::model()->findAll("CITY_ID=:CID",array(":CID"=>$id))){
				foreach($countys as $county){
					foreach($users as $user){
						if($user->unit_id == $county->COUNTY_ID && $user->unit_level == 3){
							$user->delete();
						}
					}
				}
			$status = MtsysCounty::model()->deleteAll("CITY_ID=:CID",array(":CID"=>$id));
			}
/*			if(MtsysBase::model()->findAll('CITY_ID=:CID',array(':CID' => $id))){
			$status = MtsysBase::model()->deleteAll('CITY_ID=:CID',array(':CID' => $id));
			}*/

			$bases = MtsysBase::model()->findAll("CITY_ID=:CID",array(
					":CID"=>$id));
			if(!empty($bases)){
				$status &= MtsysBase::model()->deleteAll("CITY_ID=:CID",array(
						":CID"=>$id));
				foreach($bases as $base){
					foreach($users as $user){
						if($user->unit_id == $base->BASE_ID && $user->unit_level == 4){
							$user->delete();
						}
					}
					if(BtBatterys::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status &= BtBatterys::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtBattery::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status &= BtBattery::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtRundata::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status &= BtRundata::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Pickdata::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status &= Pickdata::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Sensors::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status &= Sensors::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
				}
			}
						break;	
					case 'COUNTY':	
						$county = MtsysCounty::model()->find('COUNTY_ID=:AID',array(':AID' => $id));
			if(MtsysCounty::model()->find('COUNTY_ID=:AID',array(':AID' => $id))){
					foreach($users as $user){
						if($user->unit_id == $county->COUNTY_ID && $user->unit_level == 3){
							$user->delete();
						}
					}
				
			$status = MtsysCounty::model()->find('COUNTY_ID=:AID',array(':AID' => $id))->delete();	
			}
/*			if(MtsysBase::model()->findAll('COUNTY_ID=:AID',array(':AID' => $id))){
			$status = MtsysBase::model()->deleteAll('COUNTY_ID=:AID',array(':AID' => $id));						
			}*/

			$bases = MtsysBase::model()->findAll("COUNTY_ID=:AID",array(":AID"=>$id));			
			if(!empty($bases)){
				$status = MtsysBase::model()->deleteAll("COUNTY_ID=:AID",array(
						":AID"=>$id));
				foreach($bases as $base){
					foreach($users as $user){
						if($user->unit_id == $base->BASE_ID && $user->unit_level == 4){
							$user->delete();
						}
					}
					if(BtBatterys::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtBatterys::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtBattery::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtBattery::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtRundata::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtRundata::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Pickdata::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status = Pickdata::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Sensors::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status = Sensors::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
				}
			}
						break;
					case 'BASE':
					//删除站点数据
					$bases = MtsysBase::model()->findAll("BASE_ID=:AID",array(":AID"=>$id));			
			if(!empty($bases)){
				$status = MtsysBase::model()->deleteAll("BASE_ID=:BID",array(
						":BID"=>$id));
				foreach($bases as $base){
					foreach($users as $user){
						if($user->unit_id == $base->BASE_ID && $user->unit_level == 4){
							$user->delete();
						}
					}
					if(BtBatterys::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtBatterys::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtBattery::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtBattery::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(BtRundata::model()->findAll("BASE_N=:BID", array(":BID" => $base->BASE_ID))){
						$status = BtRundata::model()->deleteAll("BASE_N=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Pickdata::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status = Pickdata::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
					if(Sensors::model()->findAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID))){
						$status = Sensors::model()->deleteAll("BASENAME_V=:BID", array(":BID" => $base->BASE_ID));
					}
				}
			}	
					break;	
				}
			}
			
			if($status){
				$rs = array(
					'no' => '0',
					'data' => array(),
					'error' => '',
				);
			}else{
				$rs['error'] = $model->getErrors();	
			}
			echo json_encode($rs);
		}	
	}
	
	public function actionCreate()
	{
		$model=new MtsysProvince;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['MtsysProvince']))
		{
			$model->attributes=$_POST['MtsysProvince'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->PROVINCE_ID));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}
}